$(function(){
	new Vue({
		el:"#addGoodSpecPropApp",
		data:{
			textarea:"",
			fullscreenLoading:false,
			districtDefault:[],
			postOptions:[],
			
			form:{
				name:"",
				optional:"",
				weight:"5",
				categoryId:"",
				
			},
			options:[],
		},
		created:function(){
			parent.vueData.$data.hreHtml="../addGoodSpecProp/index.html";
			parent.vueData.activeIndex  = "7";
			parent.vueData.getLeftNav("7")
			parent.vueData.$data.categoryIndex = 2
			this.getAllCategory()
		},
		methods:{
			getAllCategory: function() {
				this.fullscreenLoading = true;
				let shopId = getShopId();
				axios({
					url: POSTURL.selectAllGoodCategory,
					method: 'post',
					data: {
						shopId: shopId,
						admin_grade:"1",
					}
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						for (let i = 0; i < response.data.result.length; i++) {
							let obj = response.data.result[i]
							let category = new Object();
							category.value = obj.name + "+" + obj.uid;
							category.label = obj.name;
							category.children = [];
							for (let j = 0; j < obj.secondary.length; j++) {
								let data = obj.secondary[j]
								let secondCategory = new Object();
								secondCategory.value = data.name + "+" + data.uid
								secondCategory.label = data.name
								category.children.push(secondCategory)
							}
							this.options.push(category)
						}
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			handleChange(data) {
				for (let i = 0; i < data.length; i++) {
					let obj = data[i];
					if(i ==1){
						this.form.categoryId= obj.split("+")[1]
					}
				}
			},
			//添加商品规格
			addGoodProp(){
				if(stringEmpty(this.form.name)){
					this.$message.error('规格名称不能为空!');
					return
				}
				if(stringEmpty(this.form.categoryId)){
					this.$message.error('商品分类不能为空');
					return
				}
				let index = this.textarea.indexOf("，")
				if(index>-1){
					this.$message.error('可选值不能存在中文，');
					return
				}
				let s = this.textarea.split(',')
				if(s.length >0){
					this.form.optional = s.join(',')
				}
				if(stringEmpty(this.form.optional)){
					this.$message.error('可选值不能为空');
					return
				}
				if(stringEmpty(this.form.weight)){
					this.$message.error('权重不能为空');
					return
				}
				let shopId = getShopId();
				this.form.shopId = shopId 
				this.form.admin_grade= "1"
				this.fullscreenLoading = true;
				axios({
					url: POSTURL.insertParameter,
					method: 'post',
					data: this.form
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						this.$message({
							showClose: true,
							message: response.data.message,
							type: 'success'
						});
						window.history.back(-1);
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
				
			}
		},
	})
})